package com.jdz.gxq.info.admin.role.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.role.bo.User;

public class UserServiceImpl implements UserService {

	private QueryDAO queryDAO = null;

	private UpdateDAO updateDAO = null;

	/**
	 * User检索分页用，查询记录条数
	 * 
	 * @return
	 */
	public int getUsersCount(String enterpriseName) {

		int totalCount = queryDAO.executeForObject("user.getUsersCount",
				enterpriseName, Integer.class);

		return totalCount;
	}

	/**
	 * User检索
	 * 
	 * @return
	 */
	public List<User> getUsers(Map<String, Object> params) {

		return queryDAO.executeForObjectList("user.getUsers", params);
	}

	/**
	 * 追加User
	 * 
	 * @param user
	 */
	public void insertUser(User user) {

		updateDAO.execute("user.insertUser", user);
	}

	/**
	 * 更新User
	 * 
	 * @param user
	 */
	public void updateUser(User user) {

		updateDAO.execute("user.updateUser", user);
	}

	/**
	 * 通过id得到user
	 * 
	 * @param id
	 * @return
	 */
	public User getUserById(int enterpriseId) {

		User user = queryDAO.executeForObject("user.getUserById", enterpriseId,
				User.class);

		return user;
	}

	/**
	 * 删除User
	 * 
	 * @param user
	 */
	public void deleteUser(int enterpriseId) {

		updateDAO.execute("user.deleteUserById", enterpriseId);
	}

	/**
	 * 验证userName是否重复 flag: 1 追加时验证 flag: 2 更新时验证
	 */
	public boolean checkUserName(User paramUser, int flag) {

		User user = queryDAO.executeForObject("user.getUserByName",
				paramUser.getName(), User.class);

		if (user == null) {
			return true;
		}

		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {

			if (user.getId() == paramUser.getId()) {
				return true;
			}
		}

		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}

}
